package kr.kimkisik.middle1math;

import kr.kimkisik.middle1math.danwon.DanwonMain;
import kr.kimkisik.middle1math.gyesan.GyesanMain;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.nbpcorp.mobilead.sdk.MobileAdListener;
import com.nbpcorp.mobilead.sdk.MobileAdView;

public class MainPage extends Activity implements MobileAdListener {

	ImageView danwonImage;
	ProgressDialog dialog = null;
	private MobileAdView adView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		adView = (MobileAdView) findViewById(R.id.adview1);
		adView.setListener(this);
		adView.start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (adView != null) {
			adView.destroy(); // 10
			adView = null;
		}
	}

	@Override
	public void onReceive(int err) {
		// event for receive ad
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			new AlertDialog.Builder(this)
					.setIcon(R.drawable.ic_launcher)
					.setTitle("중1수학 요점정리 및 공식계산기")
					.setMessage("해당 어플리케이션을 종료하겠습니까?")
					.setPositiveButton("종료",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									moveTaskToBack(true);
									android.os.Process
											.killProcess(android.os.Process
													.myPid());

								}
							}).setNegativeButton("취소", null).show();
		}
		return true;
	}

	public void mOnClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.gyesanbutton:
			intent.setClass(this, GyesanMain.class);
			break;
		case R.id.danwonbutton:
			intent.setClass(this, DanwonMain.class);
			break;
		case R.id.helpbutton:
			intent.setClass(this, HelpPage.class);
			break;
		}
		startActivity(intent);//
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent();
		switch (item.getItemId()) {

		case R.id.danwonmenu:
			intent.setClass(this, DanwonMain.class);
			startActivity(intent);
			return true;

		case R.id.gyesan:
			intent.setClass(this, GyesanMain.class);
			startActivity(intent);
			return true;

		}
		return false;
	}

}
